package org.onionframework.persistence.dao.processor;


import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Subqueries;
import org.onionframework.persistence.dao.CriteriaBuilder;
import org.onionframework.persistence.dao.PropertyFilterProcessor;
import org.onionframework.persistence.query.OnionQuery;

public class ExistsProcessor implements PropertyFilterProcessor {

	@Override
	public Criterion buildCriterion(String alias, String name, Object value,
			CriteriaBuilder context) {
		if (value instanceof OnionQuery) {
			OnionQuery propertyFilterList = (OnionQuery) value;

			DetachedCriteria dc = new CriteriaBuilder(context.getSession()).buildDetachedCriteria(propertyFilterList);

			dc.setProjection(Projections.id());

			return  Subqueries.exists(dc);
		} else {
			throw new RuntimeException("Exists查询只允许PropertyFilterList值");
		}
	}

}
